package com.mxx.common.netty.http.resolvers;

import com.mxx.common.netty.http.NettyMethod;
import com.mxx.common.netty.http.NettyHttpRequest;
import com.mxx.common.netty.http.NettyMethodParameter;
import io.netty.handler.codec.http.multipart.DefaultHttpDataFactory;
import io.netty.handler.codec.http.multipart.HttpDataFactory;
import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Auther: Mr. Zeng
 * @Date: 2020/8/14 23:18
 * @Description: 暂无描述
 */
@Component
public class MultipartFileResolvers implements NettyResolvers {
    private static final HttpDataFactory factory = new DefaultHttpDataFactory(true);

    @Override
    public boolean match(NettyMethod nettyMethod, NettyMethodParameter methodParameter) {
        boolean flag = methodParameter.getType().equals(HttpPostRequestDecoder.class);
        return flag;
    }

    @Override
    public Object resolvers(NettyMethod nettyMethod, NettyHttpRequest request, NettyMethodParameter parameter) throws IOException {
        HttpPostRequestDecoder requestDecoder = new HttpPostRequestDecoder(factory, request);
        requestDecoder.setDiscardThreshold(0);
        return requestDecoder;
    }
}
